package com.foogui.faw.oa;

import com.foogui.faw.oa.config.ProcessConfig;
import com.foogui.faw.oa.domain.AuditApproveDO;
import com.foogui.faw.oa.domain.AuditTrailDO;
import com.foogui.faw.oa.dto.BizModel;
import com.foogui.faw.oa.dto.StartProcessDTO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * 发起审批流服务
 *
 * @author wangxin213
 * @date 2024/01/26
 */
@Component
@Slf4j
public class BBCService {

    @Autowired
    private ProcessConfig processConfig;

    @Resource
    private ProcessService processService;


    /**
     * 开启一个审批流
     *
     * @param templateName 模板名称
     * @param submitCode   提交人域账号
     * @param bizId        业务主键
     * @param assignactors 审批人Map(key-流程节点名称，value-域账号)
     * @return {@link AuditApproveDO}
     */
    public AuditApproveDO startApprove(String templateName, String submitCode,String bizIdField, String bizId, Map<String, String> assignactors) {

        // 构造一个审批的实体
        AuditApproveDO auditApproveDO = AuditApproveDO.initiate(bizIdField,bizId,submitCode,"入账审核");

        String processInstanceId = this.doStartProcess(templateName, assignactors, auditApproveDO);
        auditApproveDO.setProcessInstanceId(processInstanceId);
        // 保存实体，将来在回调中进行获取参数并实现业务逻辑
        // auditApproveService.save(auditApproveDO);

        return auditApproveDO;
    }

    private String doStartProcess(String templateName, Map<String, String> assignactors, AuditApproveDO auditApproveDO) {

        Map<String, Object> map = new HashMap<>();
        // 这里传递的是审批实体的id
        String approveId = auditApproveDO.getId();

        map.put("code", approveId);
        map.put("type", auditApproveDO.getApplyType());


        BizModel bizModel = new BizModel();
        bizModel.setApplyHeader(map);

        StartProcessDTO startProcessDTO = StartProcessDTO.builder()
                .tenantLimitId(processConfig.getTenantLimitId())
                .appsource(processConfig.getAppsource())
                .category(processConfig.getCategory())
                .sysCode(processConfig.getSysCode())
                .procCode(processConfig.getProcCode())
                .businessKey(approveId)  // 审批流主键
                .creator(auditApproveDO.getSubmitCode()) // 域账号
                .templateName(templateName)
                .assignactors(assignactors)
                .bizModel(bizModel)
                .build();


        String processInstanceId = processService.startProcess(startProcessDTO);

        AuditTrailDO trailDO = new AuditTrailDO();
        trailDO.setAuditType("配置审核");
        trailDO.setAuditApprovedId(auditApproveDO.getId());
        trailDO.setAuditTime(new Date());
        trailDO.setSubmitCode(auditApproveDO.getSubmitCode());
        trailDO.setUserName("");
        // taskInstanceService.save(updateRecord);  // 保存审批记录

        return processInstanceId;
    }




}
